package com.yx.study.leet.questionList.questions;

import org.junit.Test;

/**
 * 43. 字符串相乘
 * https://leetcode.cn/problems/multiply-strings/
 */
public class Solution43 {
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int[] res = new int[num1.length() + num2.length()];
        for (int i = num1.length() - 1; i >= 0; i--) {
            int n1 = num1.charAt(i) - '0';
            for (int j = num2.length() - 1; j >= 0; j--) {
                int n2 = num2.charAt(j) - '0';
                int sum = (res[i + j + 1] + n1 * n2);
                res[i + j + 1] = sum % 10; //单数乘积位置
                res[i + j] += sum / 10;
            }
        }

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < res.length; i++) {
            if (i == 0 && res[i] == 0) continue;
            result.append(res[i]);
        }
        return result.toString();
    }
    @Test
    public void  test(){
        System.out.println(multiply("33", "6"));
    }

    /**
     * 官解: 卷积计算版本 使用快速傅立叶变换（Fast Fourier Transform，FFT）的方法来加速卷积
     * */

    /**
     * 01234
     *
     *   123  i
     *    45  j
     * -----
     *    15
     *   10
     *  05
     * =====
     *   12
     *  08   [i+j,i+j+]
     * 04
     * =====
     * 05535
     * */
}
